{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "04261.ipynb",
      "provenance": [],
      "include_colab_link": true
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "accelerator": "GPU"
  },
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "view-in-github",
        "colab_type": "text"
      },
      "source": [
        "<a href=\"https://colab.research.google.com/github/yananma/5_programs_per_day/blob/master/04253.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "uK_4dRlPHnDU",
        "colab_type": "text"
      },
      "source": [
        "## 2.3 自动求梯度"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "moNSfx8f0gJ0",
        "colab_type": "text"
      },
      "source": [
        "写完代码以后读 GitHub 的解释"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "t747AXojO2KU",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 67
        },
        "outputId": "823f42b9-c902-459d-cb66-9e43f763cfae"
      },
      "source": [
        "x = torch.ones(2, 2, requires_grad=True)\n",
        "print(x)\n",
        "print(x.grad_fn)"
      ],
      "execution_count": 67,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "tensor([[1., 1.],\n",
            "        [1., 1.]], requires_grad=True)\n",
            "None\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "0nkawns8SPen",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 67
        },
        "outputId": "f043cd0f-7433-4185-e732-a12911a03612"
      },
      "source": [
        "y = x + 2 \n",
        "print(y)\n",
        "print(y.grad_fn)"
      ],
      "execution_count": 68,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "tensor([[3., 3.],\n",
            "        [3., 3.]], grad_fn=<AddBackward0>)\n",
            "<AddBackward0 object at 0x7f27e39595c0>\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "5s1_qhHySSWo",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        },
        "outputId": "15e3d0e2-0d52-48ff-cc20-6a8bc190dd2f"
      },
      "source": [
        "print(x.is_leaf, y.is_leaf)"
      ],
      "execution_count": 69,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "True False\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "QTWTbxB0yU4Y",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 50
        },
        "outputId": "1184a0ff-a4b5-4d17-cd5d-e4a0c3b7d65d"
      },
      "source": [
        "z = y * y * 3 \n",
        "out = z.mean()\n",
        "print(z, out)"
      ],
      "execution_count": 71,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "tensor([[27., 27.],\n",
            "        [27., 27.]], grad_fn=<MulBackward0>) tensor(27., grad_fn=<MeanBackward0>)\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "7fqKB5uPy20i",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        },
        "outputId": "5a62ee6e-01d8-4825-84d9-d763f94e01f2"
      },
      "source": [
        "a = torch.randn(2, 2)\n",
        "a = ((a * 3) / (a - 1))\n",
        "print(a.requires_grad)"
      ],
      "execution_count": 72,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "False\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "y_D36WWLzGsU",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        },
        "outputId": "d21d98f7-0f3d-459c-8f79-eeaa09ee217c"
      },
      "source": [
        "a.requires_grad_(True)\n",
        "print(a.requires_grad)"
      ],
      "execution_count": 73,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "True\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "GhOa39zuzPLR",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        },
        "outputId": "a12564d4-7d24-49f8-cb44-ef27ae077825"
      },
      "source": [
        "b = (a * a).sum()\n",
        "print(b.grad_fn)"
      ],
      "execution_count": 74,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "<SumBackward0 object at 0x7f27df06cef0>\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "vHnLe4uwzUxr",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 50
        },
        "outputId": "3dff3ec5-874e-488b-d002-37ed8b3fcf49"
      },
      "source": [
        "out.backward()\n",
        "print(x.grad)"
      ],
      "execution_count": 75,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "tensor([[4.5000, 4.5000],\n",
            "        [4.5000, 4.5000]])\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "hOp4EpZrz0QJ",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 50
        },
        "outputId": "6bf7857a-dceb-400e-dbbb-9aded67e99e7"
      },
      "source": [
        "out2 = x.sum()\n",
        "out2.backward()\n",
        "print(x.grad)"
      ],
      "execution_count": 76,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "tensor([[5.5000, 5.5000],\n",
            "        [5.5000, 5.5000]])\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "I1VQsPPW0B3n",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 50
        },
        "outputId": "08d2b0e1-0f6d-4b6b-9b52-30c1d651c9d1"
      },
      "source": [
        "out3 = x.sum()\n",
        "x.grad.data.zero_()\n",
        "out3.backward()\n",
        "print(x.grad)"
      ],
      "execution_count": 77,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "tensor([[1., 1.],\n",
            "        [1., 1.]])\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "sM0erSTM0JoF",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 50
        },
        "outputId": "54198ae7-0274-400e-8676-4cbc9f87fed6"
      },
      "source": [
        "x = torch.tensor([1.0, 2.0, 3.0, 4.0], requires_grad=True)\n",
        "y = 2 * x \n",
        "z = y.view(2, 2)\n",
        "print(z)"
      ],
      "execution_count": 78,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "tensor([[2., 4.],\n",
            "        [6., 8.]], grad_fn=<ViewBackward>)\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "y3sLwjff07Ni",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        },
        "outputId": "27d69c87-59b7-4a43-cede-2b66490844aa"
      },
      "source": [
        "v = torch.tensor([[1.0, 0.1], [0.01, 0.001]], dtype=torch.float)\n",
        "z.backward(v)\n",
        "\n",
        "print(x.grad)"
      ],
      "execution_count": 79,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "tensor([2.0000, 0.2000, 0.0200, 0.0020])\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "Sv-2BuO41LWg",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 84
        },
        "outputId": "b7b8f912-aa43-48dd-dcf1-470d0ac92e74"
      },
      "source": [
        "x = torch.tensor(1.0, requires_grad=True)\n",
        "y1 = x ** 2 \n",
        "with torch.no_grad():\n",
        "    y2 = x ** 3 \n",
        "y3 = y1 + y2 \n",
        "\n",
        "print(x, x.requires_grad)\n",
        "print(y1, y1.requires_grad)\n",
        "print(y2, y2.requires_grad)\n",
        "print(y3, y3.requires_grad)"
      ],
      "execution_count": 80,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "tensor(1., requires_grad=True) True\n",
            "tensor(1., grad_fn=<PowBackward0>) True\n",
            "tensor(1.) False\n",
            "tensor(2., grad_fn=<AddBackward0>) True\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "Cp_JBi2j1tc4",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        },
        "outputId": "34b5e6e3-b08e-4201-b1b3-ba91de5f6e05"
      },
      "source": [
        "y3.backward()\n",
        "print(x.grad)"
      ],
      "execution_count": 81,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "tensor(2.)\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "7BwJ7CIV138S",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 84
        },
        "outputId": "27de5052-4b47-4f09-e922-9e641c889862"
      },
      "source": [
        "x = torch.ones(1, requires_grad=True)\n",
        "\n",
        "print(x.data)\n",
        "print(x.data.requires_grad)\n",
        "\n",
        "y = 2 * x \n",
        "x.data *=100 \n",
        "\n",
        "y.backward()\n",
        "print(x)\n",
        "print(x.grad)"
      ],
      "execution_count": 82,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "tensor([1.])\n",
            "False\n",
            "tensor([100.], requires_grad=True)\n",
            "tensor([2.])\n"
          ],
          "name": "stdout"
        }
      ]
    }
  ]
}